/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  com.edi.runtime.control
{
    import com.edi.runtime.Event.EDIBasicDataEvent;
    import com.edi.runtime.component.*;
    import flash.events.Event;
    
    import mx.collections.XMLListCollection;
    import mx.controls.CheckBox;
    import mx.controls.DataGrid;
    /**
     * 显示CheckBox列组件的列标题，点击时会将整个数据源中的数据全部选中，反之全不选
     * 
     */    
    public class CheckBoxHeaderRenderer extends CheckBox{   
        /**
         * 定义CheckBox列对象
         */    	
        private var _data:CheckBoxColumn;
        /**
         * 构造函数
         * 
         */        
        public function CheckBoxHeaderRenderer(){
            super();
            this.addEventListener(Event.CHANGE,onChange);//CheckBox状态变化时触发此事件
            this.label = "全选";
        }
        /**
         * 复写get data，data是整列的实例
         * @return 
         * 
         */		
        override public function get data():Object{
           return _data;//返回的是CheckBox列的对象
        }
        
        override public function set data(value:Object):void{
           
           _data = value as CheckBoxColumn;
//           trace(_data.cloumnSelected);
           selected = _data.cloumnSelected;
        }
        /**
         * 
         * @param event
         * 
         */        
        private function onChange(event:Event):void{                
           var i:int;
           var str:String;
           var evt:EDIBasicDataEvent;
           var dg:DataGrid = DataGrid(listData.owner);//获取DataGrid对象
           var column:CheckBoxColumn = dg.columns[listData.columnIndex];//获取整列的显示对象
           
           var dgDataArr:EDIDataTable = dg.dataProvider as EDIDataTable;

           column.cloumnSelected = this.selected;//更改列的全选状态
           
           column.selectItems = new Array();//重新初始化用于保存选中列的对象
           
           var type:String;
           if(this.selected)
           {	//如果为全部选中的化就将数据源赋值给column.selectItems，不是就不管他，上一步已经初始化为空
				if(dg.dataProvider)
				{
					if(dg.dataProvider as XMLListCollection)
					{
						column.selectItems = (dg.dataProvider as XMLListCollection).toArray();
	              		type=EDIBasicDataEvent.Data_Insert;
     				}
     				
				}           		
              	
           }
           else
           {
           		type=EDIBasicDataEvent.Data_Delete;
           }
			if(dgDataArr)
			{
	           if(dgDataArr.length>0){
		           if(dgDataArr[0]!=""){
			           for(i = 0; i < dgDataArr.length ; i++)
			           {	
			           		dgDataArr.cursorIndex=i;
			           		str=this.selected.toString();
			           		dgDataArr.selectRow.selected=str;
			           		/*
			           		var xml:XML=XML(dgDataArr[i]);
			           		xml.selected = str;//更改没一行的选中状态
			           		*/
			           		evt=new EDIBasicDataEvent(type);
			           		evt.value=dgDataArr[i];
			           		this.owner.dispatchEvent(evt);
			           } 
		           }  
	           } 
	           dgDataArr.refresh();//刷新数据源，达到强制更新页面显示效果的功能，防止在使用时没有在VO类中使用绑定而出现点击全选页面没有更改状态的错误      
	        }
	        else
	        {
	        	if(dg.dataProvider.length>0)
	        	{
		           for(i = 0; i < dg.dataProvider.length ; i++)
		           {	
		           		str=this.selected.toString();
		           		dg.dataProvider[i].selected=str;
		           		evt=new EDIBasicDataEvent(type);
		           		evt.value=dg.dataProvider[i];
		           		this.owner.dispatchEvent(evt);
		           } 
	           } 
	          dg.dataProvider.refresh();//刷新数据源，达到强制更新页面显示效果的功能，防止在使用时没有在VO类中使用绑定而出现点击全选页面没有更改状态的错误
	        }
        }
    }
}